package com.qq.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qq.mall.beans.Product;
import com.qq.mall.beans.Users;
import com.qq.mall.service.ProductService;
import com.qq.mall.vo.CategoryVO;
import com.qq.mall.vo.PriceImg;
import com.qq.mall.vo.ProductVO;
import com.qq.mall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/retaildemo/products")
@Api(tags = "商品管理")
public class ProductController {
    @Autowired
    ProductService productService;
    @GetMapping("/product/{id}")
    @ApiOperation("通过商品ID查询商品信息")
    public ResultVO getlist(@PathVariable("id") String id){
        ResultVO getlist = productService.getlist(id);
        return getlist;
    }
    @GetMapping("/recommend")
    @ApiOperation("根据销量推荐商品，只显示前四条")
    public List<ProductVO> RecommendThree(){
        List<ProductVO> recommend = productService.recommend();
        return recommend;
    }
    @GetMapping("/CategoryRecommend")
    @ApiOperation("各个分类推荐")
    public List<CategoryVO> CategoryRecommend(){
        return productService.categoryRecommend();
    }
    @GetMapping("/searchCate/{id}")
    @ApiOperation("通过分类ID查询商品")
    public IPage<PriceImg> searchCate(@PathVariable("id") String id, @RequestParam Integer pageNum,
                                      @RequestParam Integer pageSize){
        return  productService.getByCategoryId(id, new Page<>(pageNum, pageSize));
    }
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id) {
        return productService.removeById(id);
    }
    @GetMapping("/page")
    @ApiOperation("分页查询所有商品")
    public IPage<Product> findPage(int current, int size) {
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        Page<Product> productPage = new Page<>(current,size);
        Page<Product> page = productService.page(productPage, productQueryWrapper);
        return  page;
    }
    @GetMapping("/searchName")
    @ApiOperation("通过商品名模糊查询商品")
    public IPage<PriceImg> searchName(@RequestParam String keyword, @RequestParam Integer pageNum,
                                      @RequestParam Integer pageSize){
        return  productService.fuzzyQuery(keyword, new Page<>(pageNum, pageSize));
    }
    @PostMapping("/addProduct")
    @ApiOperation("添加商品")
    public String addproduct(@RequestBody Product product){
        productService.save(product);
        return product.getProductId();
    }
}

